package com.zenithmind.chat.controller;

import com.zenithmind.chat.pojo.vo.ChatUserVO;
import com.zenithmind.chat.pojo.vo.ChatGroupVO;
import com.zenithmind.chat.service.ChatGroupMemberService;
import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 群组成员管理控制器 - 遵循单一职责原则
 * 只负责群组成员管理的HTTP接口处理
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Slf4j
@RestController
@RequestMapping("/api/chat/group-members")
@RequiredArgsConstructor
@Validated
@Tag(name = "群组成员管理", description = "群组成员相关接口")
public class ChatGroupMemberController {

    private final ChatGroupMemberService chatGroupMemberService;

    @GetMapping("/page")
    @Operation(summary = "分页查询群组成员", description = "分页查询指定群组的成员列表")
    public Result<PageResult<ChatUserVO>> getGroupMemberPage(
            @Parameter(description = "群组ID") @RequestParam @NotBlank String groupId,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "角色") @RequestParam(required = false) Integer role,
            @Parameter(description = "关键词") @RequestParam(required = false) String keyword,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "20") Integer pageSize) {
        PageResult<ChatUserVO> result = chatGroupMemberService.getGroupMemberPage(groupId, status, role, keyword, pageNum, pageSize);
        return Result.success(result);
    }

    @GetMapping("/list")
    @Operation(summary = "查询群组成员列表", description = "查询指定群组的所有成员")
    public Result<List<ChatUserVO>> getGroupMembers(
            @Parameter(description = "群组ID") @RequestParam @NotBlank String groupId,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "角色") @RequestParam(required = false) Integer role) {
        List<ChatUserVO> members = chatGroupMemberService.getGroupMembers(groupId, status, role);
        return Result.success(members);
    }

    @PostMapping("/add")
    @Operation(summary = "添加群组成员", description = "向群组中添加新成员")
    public Result<Boolean> addMembers(
            @Parameter(description = "群组ID") @RequestParam @NotBlank String groupId,
            @RequestBody @NotEmpty List<String> userIds) {
        String operatorId = UserContext.getUserId();
        Boolean result = chatGroupMemberService.addMembers(groupId, userIds, operatorId);
        return Result.success(result);
    }

    @DeleteMapping("/remove")
    @Operation(summary = "移除群组成员", description = "从群组中移除指定成员")
    public Result<Boolean> removeMembers(
            @Parameter(description = "群组ID") @RequestParam @NotBlank String groupId,
            @RequestBody @NotEmpty List<String> userIds) {
        String operatorId = UserContext.getUserId();
        Boolean result = chatGroupMemberService.removeMembers(groupId, userIds, operatorId);
        return Result.success(result);
    }

    @PutMapping("/role")
    @Operation(summary = "设置成员角色", description = "设置群组成员的角色")
    public Result<Boolean> setMemberRole(
            @Parameter(description = "群组ID") @RequestParam @NotBlank String groupId,
            @Parameter(description = "用户ID") @RequestParam @NotBlank String userId,
            @Parameter(description = "角色") @RequestParam Integer role) {
        String operatorId = UserContext.getUserId();
        Boolean result = chatGroupMemberService.setMemberRole(groupId, userId, role, operatorId);
        return Result.success(result);
    }

    @PutMapping("/status")
    @Operation(summary = "设置成员状态", description = "设置群组成员的状态")
    public Result<Boolean> setMemberStatus(
            @Parameter(description = "群组ID") @RequestParam @NotBlank String groupId,
            @Parameter(description = "用户ID") @RequestParam @NotBlank String userId,
            @Parameter(description = "状态") @RequestParam Integer status) {
        String operatorId = UserContext.getUserId();
        Boolean result = chatGroupMemberService.setMemberStatus(groupId, userId, status, operatorId);
        return Result.success(result);
    }

    @PostMapping("/mute")
    @Operation(summary = "禁言成员", description = "禁言指定的群组成员")
    public Result<Boolean> muteMembers(
            @Parameter(description = "群组ID") @RequestParam @NotBlank String groupId,
            @RequestBody @NotEmpty List<String> userIds,
            @Parameter(description = "禁言时长(分钟)") @RequestParam Integer duration) {
        String operatorId = UserContext.getUserId();
        Boolean result = chatGroupMemberService.muteMembers(groupId, userIds, duration, operatorId);
        return Result.success(result);
    }

    @PostMapping("/unmute")
    @Operation(summary = "解除禁言", description = "解除指定成员的禁言状态")
    public Result<Boolean> unmuteMembers(
            @Parameter(description = "群组ID") @RequestParam @NotBlank String groupId,
            @RequestBody @NotEmpty List<String> userIds) {
        String operatorId = UserContext.getUserId();
        Boolean result = chatGroupMemberService.unmuteMembers(groupId, userIds, operatorId);
        return Result.success(result);
    }

    @GetMapping("/check-permission")
    @Operation(summary = "检查成员权限", description = "检查成员在群组中的权限")
    public Result<Boolean> checkMemberPermission(
            @Parameter(description = "群组ID") @RequestParam @NotBlank String groupId,
            @Parameter(description = "用户ID") @RequestParam @NotBlank String userId,
            @Parameter(description = "权限类型") @RequestParam @NotBlank String permission) {
        Boolean hasPermission = chatGroupMemberService.checkMemberPermission(groupId, userId, permission);
        return Result.success(hasPermission);
    }

    @GetMapping("/statistics")
    @Operation(summary = "群组成员统计", description = "获取群组成员的统计信息")
    public Result<Object> getMemberStatistics(
            @Parameter(description = "群组ID") @RequestParam @NotBlank String groupId) {
        Object statistics = chatGroupMemberService.getMemberStatistics(groupId);
        return Result.success(statistics);
    }

    @PostMapping("/batch-invite")
    @Operation(summary = "批量邀请成员", description = "批量邀请用户加入群组")
    public Result<Boolean> batchInviteMembers(
            @Parameter(description = "群组ID") @RequestParam @NotBlank String groupId,
            @RequestBody @NotEmpty List<String> userIds,
            @Parameter(description = "邀请消息") @RequestParam(required = false) String message) {
        String operatorId = UserContext.getUserId();
        Boolean result = chatGroupMemberService.batchInviteMembers(groupId, userIds, message, operatorId);
        return Result.success(result);
    }

    @GetMapping("/my-groups")
    @Operation(summary = "查询我加入的群组", description = "查询当前用户加入的所有群组")
    public Result<List<ChatGroupVO>> getMyGroups(
            @Parameter(description = "状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "角色") @RequestParam(required = false) Integer role) {
        String userId = UserContext.getUserId();
        List<ChatGroupVO> groups = chatGroupMemberService.getUserGroups(userId, status, role);
        return Result.success(groups);
    }

    @GetMapping("/count")
    @Operation(summary = "统计群组成员数量", description = "统计指定群组的成员数量")
    public Result<Integer> countGroupMembers(
            @Parameter(description = "群组ID") @RequestParam @NotBlank String groupId,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "角色") @RequestParam(required = false) Integer role) {
        Integer count = chatGroupMemberService.countGroupMembers(groupId, status, role);
        return Result.success(count);
    }

    @PostMapping("/auto-approve")
    @Operation(summary = "自动审批加群申请", description = "自动审批待处理的加群申请")
    public Result<Boolean> autoApproveJoinRequests(
            @Parameter(description = "群组ID") @RequestParam @NotBlank String groupId) {
        String operatorId = UserContext.getUserId();
        Boolean result = chatGroupMemberService.autoApproveJoinRequests(groupId, operatorId);
        return Result.success(result);
    }

    @PostMapping("/cleanup-inactive")
    @Operation(summary = "清理不活跃成员", description = "清理长期不活跃的群组成员")
    public Result<Integer> cleanupInactiveMembers(
            @Parameter(description = "群组ID") @RequestParam @NotBlank String groupId,
            @Parameter(description = "不活跃天数") @RequestParam Integer inactiveDays) {
        String operatorId = UserContext.getUserId();
        Integer cleanedCount = chatGroupMemberService.cleanupInactiveMembers(groupId, inactiveDays, operatorId);
        return Result.success(cleanedCount);
    }
}
